package com.example.nfcsender;

import java.nio.charset.Charset;
import java.util.Locale;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	private static final String LOG_TAG = "com.example.nfcsender.MainActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}		
	
	public void buttonClicked(View view) {
		Log.d(LOG_TAG, "buttonClicked");
		
		
		try {
			// get text
			EditText editText = (EditText) findViewById(
					R.id.editText1);
			final String sMsg = editText.getText().toString();
			// show text
			TextView textView = (TextView) findViewById(
					R.id.textView1);
			textView.setText(sMsg);
			

		    
		    // get adapter
		    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
		    
		    // check adapter
		    if (adapter == null) {
		    	Log.e(LOG_TAG, "no nfc :-(");
		    	return;
		    }
		    
		    // set callback
		    adapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
				
				@Override
				public NdefMessage createNdefMessage(NfcEvent event) {
					// create new ndef message
				    NdefRecord rec = NdefRecord.createExternal("com.example", "LighterData",
				    											("Light it up" + " " + sMsg).getBytes());
				    NdefMessage msg = new NdefMessage(rec);
				    
				    Log.d(LOG_TAG, msg.toString());
					return msg;
				}
			}, this);
		    
		} catch (Exception e) {
			Log.e(LOG_TAG, "blub", e);
		}
	}

	public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
	    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
	    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
	    byte[] textBytes = payload.getBytes(utfEncoding);
	    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
	    char status = (char) (utfBit + langBytes.length);
	    
	    byte[] data = new byte[1 + langBytes.length + textBytes.length];
	    data[0] = (byte) status;
	    System.arraycopy(langBytes, 0, data, 1, langBytes.length);
	    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
	    
	    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
	    								   NdefRecord.RTD_TEXT,
	    								   new byte[0],
	    								   data);
	    
	    return record;
	}
	
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}

	}

}
